package com.wideplay.warp.security;

import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import net.jcip.annotations.NotThreadSafe;

import javax.servlet.Filter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * On: 25/09/2007
 *
 * The Guice module that reflects the configuration invoked on SecurityService.
 *
 * @author Dhanji R. Prasanna
 * @since 1.0
 */
@NotThreadSafe
class SecurityModule extends AbstractModule {
    private final SimpleConfig config;


    public SecurityModule(SimpleConfig config) {
        this.config = config;
    }

    protected void configure() {
        //bind method security interceptor
        if (config.shouldSecureMethods())
            bindInterceptor(config.getClassMatcher(), config.getMethodMatcher(), new MethodSecurityInterceptor());

        //setup an empty list of filters to be populated on bootstrap
        bind(new TypeLiteral<List<Filter>>() { }).annotatedWith(Filters.class).toInstance(new ArrayList<Filter>());

        //bind the security butterfly component (used by Filter to locate the Warp-security filter pipeline)
        bind(FilterChainDispatch.class).asEagerSingleton();

        //bind the security service impl, which handles bootstrap of the Acegi filter pipeline
        bind(SecurityService.class).to(AcegiSecurityService.class).in(Singleton.class);

        //bind the configuration value object for reference
        bind(SimpleConfig.class).toInstance(config);
    }
}
